package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.service.edu.entity.Video;
import com.atguigu.guli.service.edu.entity.vo.VideoVo;
import com.atguigu.guli.service.edu.feign.VodMediaService;
import com.atguigu.guli.service.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author lianshao
 * @since 2021-05-16
 */
//@CrossOrigin
@Api(tags = "课时管理模块")
@RestController
@RequestMapping("/admin/edu/video")
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VodMediaService vodMediaService;

    @ApiOperation("新增课时")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "课时对象",required = true)
            @RequestBody Video video){

        boolean result = videoService.save(video);
        if(result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }

    }

    @ApiOperation("根据id修改课时信息")
    @PutMapping("update")
    public R updataVideoId(
            @ApiParam(value = "课时对象",required = true)
            @RequestBody Video video){

        boolean result = videoService.updateById(video);
        if(result){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id删除课时")
    @DeleteMapping("remove/{id}")
    public R remove(
            @ApiParam("课时id")
            @PathVariable String id){

        // 删除视频：VOD
        //在此处调用vod中的删除视频文件的接口,不想写在controller层
        Video video = videoService.getById(id);
        vodMediaService.removeVideo(video.getVideoSourceId());

        //删除视频
        videoService.removeMediaVideoById(id);

        boolean result = videoService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("根据id查询课时")
    @GetMapping("get/{id}")
    public R getById(
            @ApiParam(value = "课时id",required = true)
            @PathVariable String id){

        Video video = videoService.getById(id);

        if(video != null){
            return R.ok().data("item",video);
        }else {
            return R.error().message("数据不存在");
        }
    }

}

